/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

#include "renamedialog.h"
#include "ui_renamedialog.h"
#include <QProcess>
#include <QDebug>

ReNameDialog::ReNameDialog(QString hostname,QWidget *parent) :
    QDialog(parent),m_hostname(hostname),
    ui(new Ui::ReNameDialog)
{
    ui->setupUi(this);

    ui->lineEdit->setText(m_hostname);
    this->setWindowTitle(i18n("RenameDevice"));
    m_sys_info = new org::freedesktop::hostname1("org.freedesktop.hostname1", "/org/freedesktop/hostname1", QDBusConnection::systemBus(), this);

    if(ui->lineEdit->text().trimmed() == m_hostname.trimmed())
    {
        ui->rename_button->setEnabled(false);
    }
    else
    {
        ui->rename_button->setEnabled(true);
    }

    connect(ui->lineEdit,&QLineEdit::textChanged,
            [&]()
    {
        if(ui->lineEdit->text().trimmed() == m_hostname.trimmed())
        {
            ui->rename_button->setEnabled(false);
        }
        else
        {
            ui->rename_button->setEnabled(true);
        }
    }
    );
}

ReNameDialog::~ReNameDialog()
{
    delete ui;
}

void ReNameDialog::on_cancel_button_clicked()
{
    close();
}


void ReNameDialog::on_rename_button_clicked()
{
    m_sys_info->SetStaticHostname(ui->lineEdit->text().trimmed(),1);
    emit renamed(ui->lineEdit->text().trimmed());
    close();
}

void ReNameDialog::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_R && event->modifiers() == Qt::AltModifier)
    {
        on_rename_button_clicked();
    }
    else if(event->key() == Qt::Key_C && event->modifiers() == Qt::AltModifier)
    {
        close();
    }
}

